#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <mutex>

#include "util.hpp"
#include "log.hpp"
namespace ns_index
{

    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;

        uint64_t docId;
    };

    struct InvertItem
    {
        uint64_t docId;
        std::string word;

        int weight;
    };

    typedef std::vector<InvertItem> InvertedList;
    class Index
    {
    private:
        // static Index *instance;
    private:
        Index() {}
        Index(Index &) = delete;
        Index &operator=(const Index &) = delete;
    public:
        // static Index *getInstance()
        // {
        //     static std::mutex mtx;
        //     if (instance == nullptr)
        //     {
        //         mtx.lock();
        //         if (instance == nullptr)
        //         {
        //             instance = new Index;
        //         }
        //         mtx.unlock();
        //     }
        //     return instance;
        // }
        static Index& getInstance()
        {
            static Index instance;
            return instance;
        }
    public:
        ~Index() {}

    public:
        DocInfo *GetForwardIndex(uint64_t doc_id)
        {
            if (doc_id >= forward_index.size())
            {
                std::cerr << "error: doc_id out of range" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        InvertedList *GetInvertedList(const std::string &word)
        {
            auto iter = inverted_index.find(word);
            if (iter == inverted_index.end())
            {
                std::cerr << word << " have no Inverted List" << std::endl;
                return nullptr;
            }
            return &(iter->second);
        }
        typedef std::string path_t;
        bool BuildIndex(const path_t &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << input << " cannot open!" << std::endl;
                return false;
            }

            std::string line;
            int count = 0;
            while (std::getline(in, line))
            {
                DocInfo *doc = BuildForwardIndex(line);
                if (nullptr == doc)
                {
                    std::cerr << "build " << line << " error " << std::endl;
                    continue;
                }
                BuildInvertedIndex(*doc);
                count++;
                if(count % 100 == 0){
                    // std::cout << "doc built: " << count << std::endl;
                    LOG(INFO, "Doc Built:" + std::to_string(count));
                }
            }
            return true;
        }

    private:
        DocInfo *BuildForwardIndex(const std::string &line)
        {
            std::vector<std::string> results(3); // store title, content, url
            const static std::string sep("\3");
            ns_util::StringUtil::StringCut(line, &results, sep);
            if (results.size() != 3)
            {
                return nullptr;
            }

            forward_index.push_back({results[0], results[1], results[2], forward_index.size()});
            return &forward_index.back();
        }
        bool BuildInvertedIndex(const DocInfo &doc)
        {
            struct cnt
            {
                int content_cnt;
                int title_cnt;

                cnt() : content_cnt(0), title_cnt(0) {}
            }; // 局部类

            std::unordered_map<std::string, cnt> word_cnt;
            std::vector<std::string> title_cut;
            ns_util::JiebaUtil::CutString(doc.title, &title_cut);
            for (std::string word : title_cut)
            {
                boost::to_lower(word);
                word_cnt[word].title_cnt++;
            }

            std::vector<std::string> content_cnt;
            ns_util::JiebaUtil::CutString(doc.content, &content_cnt);
            for (std::string word : content_cnt)
            {
                boost::to_lower(word);
                word_cnt[word].content_cnt++;
            }

            const static int title_weigt = 10;
            const static int content_weight = 1;
            for (auto word : word_cnt)
            {
                InvertItem inverteditem{
                    doc.docId,
                    word.first,
                    title_weigt * word.second.title_cnt + content_weight * word.second.content_cnt};
                InvertedList &invertedlist = inverted_index[word.first];
                invertedlist.push_back(std::move(inverteditem));
            }

            return true;
        }

    private:
        std::vector<DocInfo> forward_index;
        std::unordered_map<std::string, InvertedList> inverted_index;
    };


}